home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / tar.gnu / sprite / RCS / port.c,v < prev    next >
Encoding:
Text File  |  1992-03-29  |  26.9 KB  |  1,369 lines

  1. head     1.4;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.4
  10. date     92.03.28.17.32.27;  author kupfer;  state Exp;
  11. branches ;
  12. next     1.3;
  13.  
  14. 1.3
  15. date     92.03.05.21.41.08;  author jhh;  state Exp;
  16. branches ;
  17. next     1.2;
  18.  
  19. 1.2
  20. date     90.06.28.15.35.14;  author rab;  state Exp;
  21. branches ;
  22. next     1.1;
  23.  
  24. 1.1
  25. date     90.03.21.22.03.17;  author rab;  state Exp;
  26. branches ;
  27. next     ;
  28.  
  29.  
  30. desc
  31. @@
  32.  
  33.  
  34. 1.4
  35. log
  36. @Lint.
  37. @
  38. text
  39. @/* Supporting routines which may sometimes be missing.
  40.    Copyright (C) 1988 Free Software Foundation
  41.  
  42. This file is part of GNU Tar.
  43.  
  44. GNU Tar is free software; you can redistribute it and/or modify
  45. it under the terms of the GNU General Public License as published by
  46. the Free Software Foundation; either version 1, or (at your option)
  47. any later version.
  48.  
  49. GNU Tar is distributed in the hope that it will be useful,
  50. but WITHOUT ANY WARRANTY; without even the implied warranty of
  51. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  52. GNU General Public License for more details.
  53.  
  54. You should have received a copy of the GNU General Public License
  55. along with GNU Tar; see the file COPYING.  If not, write to
  56. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  57.  
  58. /*
  59.  * @@(#)port.c 1.15    87/11/05    by John Gilmore, 1986
  60.  *
  61.  * These are routines not available in all environments.
  62.  *
  63.  * I know this introduces an extra level of subroutine calls and is
  64.  * slightly slower.  Frankly, my dear, I don't give a damn.  Let the
  65.  * Missed-Em Vee losers suffer a little.  This software is proud to
  66.  * have been written on a BSD system.
  67.  */
  68. #include <stdio.h>
  69. #include <sys/types.h>
  70. #include <sys/stat.h>
  71. #include <signal.h>
  72. #include <errno.h>
  73.  
  74. #ifdef    MSDOS
  75. #include <fcntl.h>
  76. #else
  77. #include <sys/file.h>
  78. #endif
  79.  
  80. #include "tar.h"
  81. #include "port.h"
  82.  
  83. #ifdef USG
  84. #include <string.h>
  85. #else
  86. extern size_t strlen();
  87. #endif
  88.  
  89. extern long baserec;
  90. /*
  91.  * Some people (e.g. V7) don't have a #define for these.
  92.  */
  93. #ifndef    O_BINARY
  94. #define    O_BINARY    0
  95. #endif
  96. #ifndef    O_RDONLY
  97. #define    O_RDONLY    0
  98. #endif
  99. #ifndef NULL
  100. #define NULL 0
  101. #endif
  102.  
  103. /* JF: modified so all configuration information can appear here, instead of
  104.    being scattered through the file.  Add all the machine-dependent #ifdefs
  105.    here */
  106. #undef WANT_DUMB_GETDATE/* WANT_DUMB_GETDATE --> getdate() */
  107. #undef WANT_VALLOC    /* WANT_VALLOC --> valloc() */
  108. #undef WANT_MKDIR    /* WANT_MKDIR --> mkdir() rmdir() */
  109. #undef WANT_STRING    /* WANT_STRING --> index() bcopy() bzero() bcmp() */
  110. #undef WANT_BZERO    /* WANT_BZERO --> bzero() bcmp() execlp() */
  111.             /* EMUL_OPEN3 --> open3() */
  112. #undef WANT_MKNOD    /* WANT_MKNOD --> mknod() link() chown() geteuid() */
  113. #undef WANT_UTILS    /* WANT_UTILS --> panic() ck_*() *_buffer()
  114.                merge_sort() quote_copy_string() un_quote_string() */
  115. #undef WANT_CK_PIPE    /* WANT_CK_PIPE --> ck_pipe() */
  116. #undef WANT_GETWD    /* WANT_GETWD --> getwd() */
  117. #undef WANT_STRSTR    /* WANT_STRSTR --> strstr() */
  118. #undef WANT_FTRUNCATE    /* WANT_FRTUNCATE --> frtruncate() */
  119.  
  120. /* Define only ONE of these four . . . */
  121. #undef DOPRNT_MSG    /* Define this one if you have _doprnt() and
  122.                no varargs support */
  123. #undef VARARGS_MSG    /* Define this one if you have varargs.h and
  124.                vfprintf() */
  125. #undef STDC_MSG        /* Define this one if you are using ANSI C and
  126.                and have vfprintf() */
  127. #undef LOSING_MSG    /* Define this one if you don't have any of the
  128.                above */
  129.  
  130. #ifdef USG
  131. #define WANT_STRING
  132. #define WANT_VALLOC
  133.  
  134. #if defined(sgi) && defined(mips)
  135. #define WANG_GETWD
  136. #endif
  137.  
  138. #endif
  139.  
  140. #ifdef MINIX
  141. #define WANT_BZERO
  142. #endif
  143.  
  144. #ifdef MSDOS
  145. #define WANT_STRING
  146. #define WANT_MKNOD
  147. #define WANT_UTILS
  148. #define WANT_VALLOC
  149. char TTY_NAME[] = "con";
  150. #else /* not MSDOS */
  151. char TTY_NAME[] ="/dev/tty";
  152.  
  153. #ifdef BSD42
  154. /* BSD systems should do this even if __STDC__, because
  155.    we might be using an ANSI compiler without an ANSI library.  */
  156. #define DOPRNT_MSG
  157. #else /* not BSD */
  158. #ifdef __STDC__
  159. #define STDC_MSG
  160. #else /* not ANSI C */
  161. #define LOSING_MSG
  162. #endif /* not ANSI C */
  163. #endif /* not BSD */
  164.  
  165. #define WANT_UTILS
  166. #define WANT_CK_PIPE
  167. #define WANT_STRSTR
  168. #endif /* not MSDOS */
  169.  
  170. #ifdef sprite
  171. #define WANT_DUMB_GETDATE
  172. #undef DOPRNT_MSG
  173. #ifdef mips
  174. #define VARARGS_MSG
  175. #else   
  176. #define STDC_MSG
  177. #endif
  178. #endif  /* sprite */   
  179.  
  180. /* End of system-dependent #ifdefs */
  181.  
  182. #ifdef WANT_DUMB_GETDATE
  183. /* JF a getdate() routine takes a date/time/etc and turns it into a time_t */
  184. /* This one is a quick hack I wrote in about five minutes to see if the N
  185.    option works.  Someone should replace it with one that works */
  186.  
  187. /* This getdate takes an arg of the form mm/dd/yyyy hh:mm:ss and turns it
  188.    into a time_t .  Its not well tested or anything. . .  */
  189. /* In general, you should use the getdate() supplied in getdate.y */
  190.  
  191.  
  192. /* #define OFF_FROM_GMT 18000    */    /* Change for your time zone! */
  193.  
  194. #define OFF_FROM_GMT 28800         /* For California (pacific time zone) */
  195.  
  196. /* ARGSUSED */
  197. time_t
  198. getdate(str, now)
  199. char *str;
  200. struct timeb *now;        /* ignored */
  201. {
  202.     int month,day,year,hour,minute,second;
  203.     time_t ret;
  204.     int    n;
  205.  
  206. #define SECS_PER_YEAR (365L*SECS_PER_DAY)
  207. #define SECS_PER_LEAP_YEAR (366L*SECS_PER_DAY)
  208.  
  209. #define SECS_PER_DAY (24L*60*60)
  210.     static int days_per_month[2][12] = {
  211.         31,28,31,30,31,30,31,31,30,31,30,31,
  212.         31,29,31,30,31,30,31,31,30,31,30,31
  213.     };
  214.  
  215.     static int days_per_year[2]={365,366};
  216.  
  217.     month=day=year=hour=minute=second=0;
  218.     n=sscanf(str,"%d/%d/%d %d:%d:%d",&month,&day,&year,&hour,&minute,&second);
  219.     if(n<3)
  220.         return 0;
  221.     if(year<100)
  222.         year+=1900;
  223.     if(year<1970)
  224.         return 0;
  225.     ret=0;
  226.  
  227.     ret+=OFF_FROM_GMT;
  228.  
  229.     for(n=1970;n<year;n++)
  230.         if(n%4==0 && n%400!=0)
  231.             ret+=SECS_PER_LEAP_YEAR;
  232.         else
  233.             ret+=SECS_PER_YEAR;
  234.  
  235.     month--;
  236.     for(n=0;n<month;n++) {
  237.         if(year%4==0 && year%400!=0)
  238.             ret+=SECS_PER_DAY*days_per_month[1][n];
  239.         else
  240.             ret+=SECS_PER_DAY*days_per_month[0][n];
  241.     }
  242.     ret+=SECS_PER_DAY*(day-1);
  243.     ret+=second+minute*60+hour*60*60;
  244.     return ret;
  245. }
  246. #endif
  247.  
  248. #ifdef WANT_VALLOC
  249. /*
  250.  * valloc() does a malloc() on a page boundary.  On some systems,
  251.  * this can make large block I/O more efficient.
  252.  */
  253. char *
  254. valloc (size)
  255.     unsigned size;
  256. {
  257.     extern char *malloc ();
  258.     return (malloc (size));
  259. }
  260. #endif
  261. /*
  262.  *                NMKDIR.C
  263.  *
  264.  * Written by Robert Rother, Mariah Corporation, August 1985. 
  265.  *
  266.  * I wrote this out of shear disgust with myself because I couldn't
  267.  * figure out how to do this in /bin/sh.
  268.  *
  269.  * If you want it, it's yours.  All I ask in return is that if you
  270.  * figure out how to do this in a Bourne Shell script you send me
  271.  * a copy.
  272.  *                    sdcsvax!rmr or rmr@@uscd
  273. *
  274. * Severely hacked over by John Gilmore to make a 4.2BSD compatible
  275. * subroutine.    11Mar86; hoptoad!gnu
  276. *
  277. * Modified by rmtodd@@uokmax 6-28-87 -- when making an already existing dir,
  278. * subroutine didn't return EEXIST.  It does now.
  279. */
  280.  
  281. /*
  282.  * Make a directory.  Compatible with the mkdir() system call on 4.2BSD.
  283.  */
  284. #ifdef WANT_MKDIR
  285. int
  286. mkdir(dpath, dmode)
  287.     char *dpath;
  288.     int dmode;
  289. {
  290.     int cpid, status;
  291.     struct stat statbuf;
  292.     extern int errno;
  293.  
  294.     if (stat(dpath,&statbuf) == 0) {
  295.         errno = EEXIST;        /* Stat worked, so it already exists */
  296.         return -1;
  297.     }
  298.  
  299.     /* If stat fails for a reason other than non-existence, return error */
  300.     if (errno != ENOENT) return -1; 
  301.  
  302.     switch (cpid = fork()) {
  303.  
  304.     case -1:            /* Error in fork() */
  305.         return(-1);        /* Errno is set already */
  306.  
  307.     case 0:                /* Child process */
  308.         /*
  309.          * Cheap hack to set mode of new directory.  Since this
  310.          * child process is going away anyway, we zap its umask.
  311.          * FIXME, this won't suffice to set SUID, SGID, etc. on this
  312.          * directory.  Does anybody care?
  313.          */
  314.         status = umask(0);    /* Get current umask */
  315.         status = umask(status | (0777 & ~dmode)); /* Set for mkdir */
  316.         execl("/bin/mkdir", "mkdir", dpath, (char *)0);
  317.         _exit(-1);        /* Can't exec /bin/mkdir */
  318.     
  319.     default:            /* Parent process */
  320.         while (cpid != wait(&status)) ;    /* Wait for kid to finish */
  321.     }
  322.  
  323.     if (TERM_SIGNAL(status) != 0 || TERM_VALUE(status) != 0) {
  324.         errno = EIO;        /* We don't know why, but */
  325.         return -1;        /* /bin/mkdir failed */
  326.     }
  327.  
  328.     return 0;
  329. }
  330. int
  331. rmdir(dpath)
  332.     char *dpath;
  333. {
  334.     int cpid, status;
  335.     struct stat statbuf;
  336.     extern int errno;
  337.  
  338.     if (stat(dpath,&statbuf) != 0) {
  339.         /* Stat just set errno.  We don't have to */
  340.         return -1;
  341.     }
  342.  
  343.     switch (cpid = fork()) {
  344.  
  345.     case -1:            /* Error in fork() */
  346.         return(-1);        /* Errno is set already */
  347.  
  348.     case 0:                /* Child process */
  349.         execl("/bin/rmdir", "rmdir", dpath, (char *)0);
  350.         _exit(-1);        /* Can't exec /bin/mkdir */
  351.     
  352.     default:            /* Parent process */
  353.         while (cpid != wait(&status)) ;    /* Wait for kid to finish */
  354.     }
  355.  
  356.     if (TERM_SIGNAL(status) != 0 || TERM_VALUE(status) != 0) {
  357.         errno = EIO;        /* We don't know why, but */
  358.         return -1;        /* /bin/mkdir failed */
  359.     }
  360.  
  361.     return 0;
  362. }
  363. #endif
  364.  
  365. #ifdef WANT_STRING
  366. /*
  367.  * Translate V7 style into Sys V style.
  368.  */
  369. #include <string.h>
  370. #include <memory.h>
  371.  
  372. char *
  373. index (s, c)
  374.     char *s;
  375.     int c;
  376. {
  377.     return (strchr (s, c));
  378. }
  379.  
  380. char *
  381. rindex(s,c)
  382. char *s;
  383. int c;
  384. {
  385.     return strrchr(s,c);
  386. }
  387.  
  388. void
  389. bcopy (s1, s2, n)
  390.     char *s1, *s2;
  391.     int n;
  392. {
  393.     (void) memcpy (s2, s1, n);
  394. }
  395.  
  396. void
  397. bzero (s1, n)
  398.     char *s1;
  399.     int n;
  400. {
  401.     (void) memset(s1, 0, n);
  402. }
  403.  
  404. int
  405. bcmp(s1, s2, n)
  406.     char    *s1, *s2;
  407.     int    n;
  408. {
  409.     return memcmp(s1, s2, n);
  410. }
  411. #endif
  412.  
  413. #ifdef WANT_BZERO
  414. /* Minix has bcopy but not bzero, and no memset.  Thanks, Andy. */
  415. void
  416. bzero (s1, n)
  417.     register char *s1;
  418.     register int n;
  419. {
  420.     while (n--) *s1++ = '\0';
  421. }
  422.  
  423. /* It also has no bcmp() */
  424. int
  425. bcmp (s1, s2, n) 
  426.     register char *s1,*s2;
  427.     register int n;
  428. {
  429.     for ( ; n-- ; ++s1, ++s2) {
  430.         if (*s1 != *s2) return *s1 - *s2;
  431.     }
  432.     return 0;
  433. }
  434.  
  435. /*
  436.  * Groan, Minix doesn't have execlp either!
  437.  *
  438.  * execlp(file,arg0,arg1...argn,(char *)NULL)
  439.  * exec a program, automatically searching for the program through
  440.  * all the directories on the PATH.
  441.  *
  442.  * This version is naive about variable argument lists, it assumes
  443.  * a straightforward C calling sequence.  If your system has odd stacks
  444.  * *and* doesn't have execlp, YOU get to fix it.
  445.  */
  446. int
  447. execlp(filename, arg0)
  448.     char *filename, *arg0;
  449. {
  450.     register char *p, *path;    
  451.     register char *fnbuffer;
  452.     char **argstart = &arg0;
  453.     struct stat statbuf;
  454.     extern char **environ;
  455.     extern int errno;
  456.     extern char *malloc(), *getenv(), *index();
  457.  
  458.     if ((p = getenv("PATH")) == NULL) {
  459.         /* couldn't find path variable -- try to exec given filename */
  460.         return execve(filename, argstart, environ);
  461.     }
  462.  
  463.     /*
  464.      * make a place to build the filename.  We malloc larger than we
  465.      * need, but we know it will fit in this.
  466.      */
  467.     fnbuffer = malloc( strlen(p) + 1 + strlen(filename) );
  468.     if (fnbuffer == NULL) {
  469.         errno = ENOMEM;
  470.         return -1;
  471.     }
  472.  
  473.     /*
  474.      * try each component of the path to see if the file's there
  475.      * and executable.
  476.      */
  477.     for (path = p ; path ; path = p) {
  478.         /* construct full path name to try */
  479.         if ((p = index(path,':')) == NULL) {
  480.             strcpy(fnbuffer, path);
  481.         } else {
  482.             strncpy(fnbuffer, path, p-path);
  483.             fnbuffer[p-path] = '\0';
  484.             p++;        /* Skip : for next time */
  485.         }
  486.         if (strlen(fnbuffer) != 0)
  487.             strcat(fnbuffer,"/");
  488.         strcat(fnbuffer,filename);
  489.  
  490.         /* check to see if file is there and is a normal file */
  491.         if (stat(fnbuffer, &statbuf) < 0) {
  492.             if (errno == ENOENT)
  493.                 continue; /* file not there,keep on looking */
  494.             else
  495.                 goto fail; /* failed for some reason, return */
  496.         }
  497.         if ( (statbuf.st_mode & S_IFMT) != S_IFREG) continue;
  498.  
  499.         if (execve(fnbuffer, argstart, environ) < 0
  500.             && errno != ENOENT
  501.             && errno != ENOEXEC) {
  502.             /* failed, for some other reason besides "file
  503.              * not found" or "not a.out format"
  504.              */
  505.             goto fail;
  506.         }
  507.  
  508.         /*
  509.          * If we got error ENOEXEC, the file is executable but is
  510.          * not an object file.  Try to execute it as a shell script,
  511.          * returning error if we can't execute /bin/sh.
  512.          *
  513.          * FIXME, this code is broken in several ways.  Shell
  514.          * scripts should not in general be executed by the user's
  515.          * SHELL variable program.  On more mature systems, the
  516.          * script can specify with #!/bin/whatever.  Also, this
  517.          * code clobbers argstart[-1] if the exec of the shell
  518.          * fails.
  519.          */
  520.         if (errno == ENOEXEC) {
  521.             char *shell;
  522.  
  523.             /* Try to execute command "sh arg0 arg1 ..." */
  524.             if ((shell = getenv("SHELL")) == NULL)
  525.                 shell = "/bin/sh";
  526.             argstart[-1] = shell;
  527.             argstart[0] = fnbuffer;
  528.             execve(shell, &argstart[-1], environ);
  529.             goto fail;    /* Exec didn't work */
  530.         }
  531.  
  532.         /* 
  533.          * If we succeeded, the execve() doesn't return, so we
  534.          * can only be here is if the file hasn't been found yet.
  535.          * Try the next place on the path.
  536.          */
  537.     }
  538.  
  539.     /* all attempts failed to locate the file.  Give up. */
  540.     errno = ENOENT;
  541.  
  542. fail:
  543.     free(fnbuffer);
  544.     return -1;
  545. }
  546. #endif
  547.  
  548.  
  549. #ifdef EMUL_OPEN3
  550. #include "open3.h"
  551. /*
  552.  * open3 -- routine to emulate the 3-argument open system
  553.  * call that is present in most modern Unix systems.
  554.  * This version attempts to support all the flag bits except for O_NDELAY
  555.  * and O_APPEND, which are silently ignored.  The emulation is not as efficient
  556.  * as the real thing (at worst, 4 system calls instead of one), but there's
  557.  * not much I can do about that.
  558.  *
  559.  * Written 6/10/87 by rmtodd@@uokmax
  560.  *
  561.  * open3(path, flag, mode)
  562.  * Attempts to open the file specified by
  563.  * the given pathname.  The following flag bits (#defined in tar.h)
  564.  * specify options to the routine:
  565.  *    O_RDONLY    file open for read only
  566.  *    O_WRONLY    file open for write only
  567.  *    O_RDWR        file open for both read & write
  568.  * (Needless to say, you should only specify one of the above).
  569.  *     O_CREAT        file is created with specified mode if it needs to be.
  570.  *    O_TRUNC        if file exists, it is truncated to 0 bytes
  571.  *    O_EXCL        used with O_CREAT--routine returns error if file exists
  572.  * Function returns file descriptor if successful, -1 and errno if not.
  573.  */
  574.  
  575. /*
  576.  * array to give arguments to access for various modes
  577.  * FIXME, this table depends on the specific integer values of O_XXX,
  578.  * and also contains integers (args to 'access') that should be #define's.
  579.  */
  580. static int modes[] =
  581.     {
  582.         04, /* O_RDONLY */
  583.         02, /* O_WRONLY */
  584.         06, /* O_RDWR */
  585.         06, /* invalid but we'd better cope -- O_WRONLY+O_RDWR */
  586.     };
  587.  
  588. /* Shut off the automatic emulation of open(), we'll need it. */
  589. #undef open
  590.  
  591. int
  592. open3(path, flags, mode)
  593. char *path;
  594. int flags, mode;
  595. {
  596.     extern int errno;
  597.     int exists = 1;
  598.     int call_creat = 0;
  599.     int fd;
  600.     /*
  601.      * We actually do the work by calling the open() or creat() system
  602.      * call, depending on the flags.  Call_creat is true if we will use 
  603.      * creat(), false if we will use open().
  604.      */
  605.  
  606.     /*
  607.      * See if the file exists and is accessible in the requested mode. 
  608.      *
  609.      * Strictly speaking we shouldn't be using access, since access checks
  610.      * against real uid, and the open call should check against euid.
  611.      * Most cases real uid == euid, so it won't matter.   FIXME.
  612.      * FIXME, the construction "flags & 3" and the modes table depends
  613.      * on the specific integer values of the O_XXX #define's.  Foo!
  614.      */
  615.     if (access(path,modes[flags & 3]) < 0) {
  616.         if (errno == ENOENT) {
  617.             /* the file does not exist */
  618.             exists = 0;
  619.         } else {
  620.             /* probably permission violation */
  621.             if (flags & O_EXCL) {
  622.                 /* Oops, the file exists, we didn't want it. */
  623.                 /* No matter what the error, claim EEXIST. */
  624.                 errno = EEXIST;
  625.             }
  626.             return -1;
  627.         }
  628.     }
  629.  
  630.     /* if we have the O_CREAT bit set, check for O_EXCL */
  631.     if (flags & O_CREAT) {
  632.         if ((flags & O_EXCL) && exists) {
  633.             /* Oops, the file exists and we didn't want it to. */
  634.             errno = EEXIST;
  635.             return -1;
  636.         }
  637.         /*
  638.          * If the file doesn't exist, be sure to call creat() so that
  639.          * it will be created with the proper mode.
  640.          */
  641.         if (!exists) call_creat = 1;
  642.     } else {
  643.         /* If O_CREAT isn't set and the file doesn't exist, error. */
  644.         if (!exists) {
  645.             errno = ENOENT;
  646.             return -1;
  647.         }
  648.     }
  649.  
  650.     /*
  651.      * If the O_TRUNC flag is set and the file exists, we want to call
  652.      * creat() anyway, since creat() guarantees that the file will be
  653.      * truncated and open()-for-writing doesn't.
  654.      * (If the file doesn't exist, we're calling creat() anyway and the
  655.      * file will be created with zero length.)
  656.      */
  657.     if ((flags & O_TRUNC) && exists) call_creat = 1;
  658.     /* actually do the call */
  659.     if (call_creat) {
  660.         /*
  661.          * call creat.  May have to close and reopen the file if we
  662.          * want O_RDONLY or O_RDWR access -- creat() only gives
  663.          * O_WRONLY.
  664.          */
  665.         fd = creat(path,mode);
  666.         if (fd < 0 || (flags & O_WRONLY)) return fd;
  667.         if (close(fd) < 0) return -1;
  668.         /* Fall out to reopen the file we've created */
  669.     }
  670.  
  671.     /*
  672.      * calling old open, we strip most of the new flags just in case.
  673.      */
  674.     return open(path, flags & (O_RDONLY|O_WRONLY|O_RDWR|O_BINARY));
  675. }
  676. #endif
  677.  
  678. #ifdef    WANT_MKNOD
  679. /* Fake mknod by complaining */
  680. int
  681. mknod(path, mode, dev)
  682.     char        *path;
  683.     unsigned short    mode;
  684.     dev_t        dev;
  685. {
  686.     extern int    errno;
  687.     int        fd;
  688.     
  689.     errno = ENXIO;        /* No such device or address */
  690.     return -1;        /* Just give an error */
  691. }
  692.  
  693. /* Fake links by copying */
  694. int
  695. link(path1, path2)
  696.     char        *path1;
  697.     char        *path2;
  698. {
  699.     char    buf[256];
  700.     int    ifd, ofd;
  701.     int    nrbytes;
  702.     int    nwbytes;
  703.  
  704.     fprintf(stderr, "%s: %s: cannot link to %s, copying instead\n",
  705.         tar, path1, path2);
  706.     if ((ifd = open(path1, O_RDONLY|O_BINARY)) < 0)
  707.         return -1;
  708.     if ((ofd = creat(path2, 0666)) < 0)
  709.         return -1;
  710.     setmode(ofd, O_BINARY);
  711.     while ((nrbytes = read(ifd, buf, sizeof(buf))) > 0) {
  712.         if ((nwbytes = write(ofd, buf, nrbytes)) != nrbytes) {
  713.             nrbytes = -1;
  714.             break;
  715.         }
  716.     }
  717.     /* Note use of "|" rather than "||" below: we want to close
  718.      * the files even if an error occurs.
  719.      */
  720.     if ((nrbytes < 0) | (0 != close(ifd)) | (0 != close(ofd))) {
  721.         unlink(path2);
  722.         return -1;
  723.     }
  724.     return 0;
  725. }
  726.  
  727. /* everyone owns everything on MS-DOS (or is it no one owns anything?) */
  728. int
  729. chown(path, uid, gid)
  730.     char    *path;
  731.     int    uid;
  732.     int    gid;
  733. {
  734.     return 0;
  735. }
  736.  
  737. int
  738. geteuid()
  739. {
  740.     return 0;
  741. }
  742. #endif    /* WANT_MKNOD */
  743.  
  744. #ifdef WANT_UTILS
  745. /* Stash argv[0] here so panic will know what the program is called */
  746. char *myname = 0;
  747.  
  748. void
  749. panic(s)
  750. char *s;
  751. {
  752.     if(myname)
  753.         fprintf(stderr,"PANIC %s:",myname);
  754.     fprintf(stderr,s);
  755.     putc('\n',stderr);
  756.     exit(12);
  757. }
  758.  
  759. char *
  760. ck_realloc(ptr,size)
  761. char *ptr;
  762. size_t size;
  763. {
  764.     char *ret;
  765.     char *realloc();
  766.  
  767.     ret=realloc(ptr,size);
  768.     if(ret==0)
  769.         panic("Couldn't re-allocate memory");
  770.     return ret;
  771. }
  772.  
  773.  
  774. /* Implement a variable sized buffer of 'stuff'.  We don't know what it is,
  775.    nor do we care, as long as it doesn't mind being aligned on a char boundry.
  776.  */
  777.  
  778. struct buffer {
  779.     int    allocated;
  780.     int    length;
  781.     char    *b;
  782. };
  783.  
  784. #define MIN_ALLOCATE 50
  785.  
  786. char *
  787. init_buffer()
  788. {
  789.     struct buffer *b;
  790.     char *ck_malloc();
  791.  
  792.     b=(struct buffer *)ck_malloc(sizeof(struct buffer));
  793.     b->allocated=MIN_ALLOCATE;
  794.     b->b=(char *)ck_malloc(MIN_ALLOCATE);
  795.     b->length=0;
  796.     return (char *)b;
  797. }
  798.  
  799. void
  800. flush_buffer(bb)
  801. char *bb;
  802. {
  803.     struct buffer *b;
  804.  
  805.     b=(struct buffer *)bb;
  806.     free(b->b);
  807.     b->b=0;
  808.     b->allocated=0;
  809.     b->length=0;
  810.     free((void *)b);
  811. }
  812.  
  813. void
  814. add_buffer(bb,p,n)
  815. char *bb;
  816. char *p;
  817. int n;
  818. {
  819.     struct buffer *b;
  820.  
  821.     b=(struct buffer *)bb;
  822.     if(b->length+n>b->allocated) {
  823.         b->allocated*=2;
  824.         b->b=(char *)ck_realloc(b->b,b->allocated);
  825.     }
  826.     bcopy(p,b->b+b->length,n);
  827.     b->length+=n;
  828. }
  829.  
  830. char *
  831. get_buffer(bb)
  832. char *bb;
  833. {
  834.     struct buffer *b;
  835.  
  836.     b=(struct buffer *)bb;
  837.     return b->b;
  838. }
  839.  
  840. char *
  841. merge_sort(list,n,off,cmp)
  842. char *list;
  843. int (*cmp)();
  844. unsigned n;
  845. int off;
  846. {
  847.     char *ret;
  848.  
  849.     char *alist,*blist;
  850.     unsigned alength,blength;
  851.  
  852.     char *tptr;
  853.     int tmp;
  854.     char **prev;
  855. #define NEXTOF(ptr)    (* ((char **)(((char *)(ptr))+off) ) )
  856.     if(n==1)
  857.         return list;
  858.     if(n==2) {
  859.         if((*cmp)(list,NEXTOF(list))>0) {
  860.             ret=NEXTOF(list);
  861.             NEXTOF(ret)=list;
  862.             NEXTOF(list)=0;
  863.             return ret;
  864.         }
  865.         return list;
  866.     }
  867.     alist=list;
  868.     alength=(n+1)/2;
  869.     blength=n/2;
  870.     for(tptr=list,tmp=(n-1)/2;tmp;tptr=NEXTOF(tptr),tmp--)
  871.         ;
  872.     blist=NEXTOF(tptr);
  873.     NEXTOF(tptr)=0;
  874.  
  875.     alist=merge_sort(alist,alength,off,cmp);
  876.     blist=merge_sort(blist,blength,off,cmp);
  877.     prev = &ret;
  878.     for(;alist && blist;) {
  879.         if((*cmp)(alist,blist)<0) {
  880.             tptr=NEXTOF(alist);
  881.             *prev = alist;
  882.             prev = &(NEXTOF(alist));
  883.             alist=tptr;
  884.         } else {
  885.             tptr=NEXTOF(blist);
  886.             *prev = blist;
  887.             prev = &(NEXTOF(blist));
  888.             blist=tptr;
  889.         }
  890.     }
  891.     if(alist)
  892.         *prev = alist;
  893.     else
  894.         *prev = blist;
  895.  
  896.     return ret;
  897. }
  898.  
  899. char *
  900. ck_malloc(size)
  901. size_t size;
  902. {
  903.     char *ret;
  904.     char *malloc();
  905.  
  906.     ret=malloc(size);
  907.     if(ret==0)
  908.         panic("Couldn't allocate memory");
  909.     return ret;
  910. }
  911.  
  912. void
  913. ck_close(fd)
  914. int fd;
  915. {
  916.     if(close(fd)<0) {
  917.         msg_perror("can't close a file #%d",fd);
  918.         exit(EX_SYSTEM);
  919.     }
  920. }
  921.  
  922. #include <ctype.h>
  923.  
  924. /* Quote_copy_string is like quote_string, but instead of modifying the
  925.    string in place, it malloc-s a copy  of the string, and returns that.
  926.    If the string does not have to be quoted, it returns the NULL string.
  927.    The allocated copy can, of course, be freed with free() after the
  928.    caller is done with it.
  929.  */
  930. char *
  931. quote_copy_string(string)
  932. char *string;
  933. {
  934.     char    *from_here;
  935.     char    *to_there = 0;
  936.     char    *copy_buf = 0;
  937.     int    c;
  938.     int    copying = 0;
  939.  
  940.     from_here=string;
  941.     while(*from_here) {
  942.         c= *from_here++;
  943.         if(c=='\\') {
  944.             if(!copying) {
  945.                 int n;
  946.  
  947.                 n=(from_here-string)-1;
  948.                 copying++;
  949.                 copy_buf=(char *)malloc(n+3+strlen(from_here)*4);
  950.                 if(!copy_buf)
  951.                     return 0;
  952.                 bcopy(string,copy_buf,n);
  953.                 to_there=copy_buf+n;
  954.             }
  955.             *to_there++='\\';
  956.             *to_there++='\\';
  957.         } else if(isprint(c)) {
  958.             if(copying)
  959.                 *to_there++= c;
  960.         } else {
  961.             if(!copying) {
  962.                 int    n;
  963.  
  964.                 n=(from_here-string)-1;
  965.                 copying++;
  966.                 copy_buf=(char *)malloc(n+3+strlen(from_here)*4);
  967.                 if(!copy_buf)
  968.                     return 0;
  969.                 bcopy(string,copy_buf,n);
  970.                 to_there=copy_buf+n;
  971.             }
  972.             *to_there++='\\';
  973.             if(c=='\n') *to_there++='n';
  974.             else if(c=='\t') *to_there++='t';
  975.             else if(c=='\f') *to_there++='f';
  976.             else if(c=='\b') *to_there++='b';
  977.             else if(c=='\r') *to_there++='r';
  978.             else if(c=='\177') *to_there++='?';
  979.             else {
  980.                 to_there[0]=(c>>6)+'0';
  981.                 to_there[1]=((c>>3)&07)+'0';
  982.                 to_there[2]=(c&07)+'0';
  983.                 to_there+=3;
  984.             }
  985.         }
  986.     }
  987.     if(copying) {
  988.         *to_there='\0';
  989.         return copy_buf;
  990.     }
  991.     return (char *)0;
  992. }
  993.  
  994.  
  995. /* Un_quote_string takes a quoted c-string (like those produced by
  996.    quote_string or quote_copy_string and turns it back into the
  997.    un-quoted original.  This is done in place.
  998.  */
  999.  
  1000. /* There is no un-quote-copy-string.  Write it yourself */
  1001.  
  1002. char *un_quote_string(string)
  1003. char *string;
  1004. {
  1005.     char *ret;
  1006.     char *from_here;
  1007.     char *to_there;
  1008.     int    tmp;
  1009.  
  1010.     ret=string;
  1011.     to_there=string;
  1012.     from_here=string;
  1013.     while(*from_here) {
  1014.         if(*from_here!='\\') {
  1015.             if(from_here!=to_there)
  1016.                 *to_there++= *from_here++;
  1017.             else
  1018.                 from_here++,to_there++;
  1019.             continue;
  1020.         }
  1021.         switch(*++from_here) {
  1022.         case '\\':
  1023.             *to_there++= *from_here++;
  1024.             break;
  1025.         case 'n':
  1026.             *to_there++= '\n';
  1027.             from_here++;
  1028.             break;
  1029.         case 't':
  1030.             *to_there++= '\t';
  1031.             from_here++;
  1032.             break;
  1033.         case 'f':
  1034.             *to_there++= '\f';
  1035.             from_here++;
  1036.             break;
  1037.         case 'b':
  1038.             *to_there++= '\b';
  1039.             from_here++;
  1040.             break;
  1041.         case 'r':
  1042.             *to_there++= '\r';
  1043.             from_here++;
  1044.             break;
  1045.         case '?':
  1046.             *to_there++= 0177;
  1047.             from_here++;
  1048.             break;
  1049.         case '0':
  1050.         case '1':
  1051.         case '2':
  1052.         case '3':
  1053.         case '4':
  1054.         case '5':
  1055.         case '6':
  1056.         case '7':
  1057.             tmp= *from_here - '0';
  1058.             from_here++;
  1059.             if(*from_here<'0' || *from_here>'7') {
  1060.                 *to_there++= tmp;
  1061.                 break;
  1062.             }
  1063.             tmp= tmp*8 + *from_here-'0';
  1064.             from_here++;
  1065.             if(*from_here<'0' || *from_here>'7') {
  1066.                 *to_there++= tmp;
  1067.                 break;
  1068.             }
  1069.             tmp=tmp*8 + *from_here-'0';
  1070.             from_here++;
  1071.             *to_there=tmp;
  1072.             break;
  1073.         default:
  1074.             ret=0;
  1075.             *to_there++='\\';
  1076.             *to_there++= *from_here++;
  1077.             break;
  1078.         }
  1079.     }
  1080.     *to_there++='\0';
  1081.     return ret;
  1082. }
  1083. #endif
  1084.  
  1085. #ifdef WANT_CK_PIPE
  1086. void ck_pipe(pipes)
  1087. int *pipes;
  1088. {
  1089.     if(pipe(pipes)<0) {
  1090.         msg_perror("can't open a pipe");
  1091.         exit(EX_SYSTEM);
  1092.     }
  1093. }
  1094.  
  1095. #endif
  1096.  
  1097. #ifdef WANT_GETWD
  1098. char *
  1099. getwd(path)
  1100. char *path;
  1101. {
  1102.     FILE *fp;
  1103.     FILE *popen();
  1104.  
  1105.     fp=popen("pwd","r");
  1106.     if(!fp)
  1107.         return 0;
  1108.     if(!fgets(path,100,fp))
  1109.         return 0;
  1110.     if(!pclose(fp))
  1111.         return 0;
  1112.     return path;
  1113. }
  1114. #endif /* WANT_CK_PIPE */
  1115.  
  1116.  
  1117. #ifdef WANT_STRSTR
  1118.  
  1119. /*
  1120.  * strstr - find first occurrence of wanted in s
  1121.  */
  1122.  
  1123. char *                /* found string, or NULL if none */
  1124. strstr(s, wanted)
  1125. char *s;
  1126. char *wanted;
  1127. {
  1128.     register char *scan;
  1129.     register size_t len;
  1130.     register char firstc;
  1131.     extern int strcmp();
  1132.  
  1133.     /*
  1134.      * The odd placement of the two tests is so "" is findable.
  1135.      * Also, we inline the first char for speed.
  1136.      * The ++ on scan has been moved down for optimization.
  1137.      */
  1138.     firstc = *wanted;
  1139.     len = strlen(wanted);
  1140.     for (scan = s; *scan != firstc || strncmp(scan, wanted, len) != 0; )
  1141.         if (*scan++ == '\0')
  1142.             return (char *)0;
  1143.     return scan;
  1144. }
  1145. #endif
  1146.  
  1147. #ifdef WANT_FTRUNCATE
  1148.  
  1149. int
  1150. ftruncate(fd, length)
  1151. int fd;
  1152. off_t length;
  1153. {
  1154.     errno = EIO;
  1155.     return -1;
  1156. }
  1157.  
  1158. #endif
  1159.  
  1160.  
  1161.  
  1162.  
  1163. extern FILE *msg_file;
  1164.  
  1165. #ifdef STDC_MSG
  1166. #include <stdarg.h>
  1167.  
  1168. #ifdef lint
  1169. /* VARARGS */
  1170. void
  1171. msg(str)
  1172.     char *str;
  1173. {
  1174.     str = str;
  1175. }
  1176.  
  1177. #else /* lint */
  1178.  
  1179. void
  1180. msg(char *str,...)
  1181. {
  1182.     va_list args;
  1183.  
  1184.     va_start(args,str);
  1185.     fflush(msg_file);
  1186.     fprintf(stderr,"%s: ",tar);
  1187.     if(f_sayblock)
  1188.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1189.     vfprintf(stderr,str,args);
  1190.     va_end(args);
  1191.     putc('\n',stderr);
  1192.     fflush(stderr);
  1193. }
  1194.  
  1195. void
  1196. msg_perror(char *str,...)
  1197. {
  1198.     va_list args;
  1199.     int save_e;
  1200.     extern int errno;
  1201.  
  1202.     save_e=errno;
  1203.     fflush(msg_file);
  1204.     fprintf(stderr,"%s: ",tar);
  1205.     if(f_sayblock)
  1206.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1207.     va_start(args,str);
  1208.     vfprintf(stderr,str,args);
  1209.     va_end(args);
  1210.     errno=save_e;
  1211.     perror(" ");
  1212.     fflush(stderr);
  1213. }
  1214. #endif /* lint */
  1215. #endif /* STDC_MSG */
  1216.  
  1217. #ifdef VARARGS_MSG
  1218. #include <varargs.h>
  1219. void msg(str,va_alist)
  1220. char *str;
  1221. va_dcl
  1222. {
  1223.     va_list args;
  1224.  
  1225.     fflush(msg_file);
  1226.     fprintf(stderr,"%s: ",tar);
  1227.     if(f_sayblock)
  1228.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1229.     va_start(args);
  1230.     vfprintf(stderr,str,args);
  1231.     va_end(args);
  1232.     putc('\n',stderr);
  1233.     fflush(stderr);
  1234. }
  1235.  
  1236. void msg_perror(str,va_alist)
  1237. char *str;
  1238. va_dcl
  1239. {
  1240.     va_list args;
  1241.     int save_e;
  1242.     extern int errno;
  1243.  
  1244.     save_e=errno;
  1245.     fflush(msg_file);
  1246.     fprintf(stderr,"%s: ",tar);
  1247.     if(f_sayblock)
  1248.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1249.     va_start(args);
  1250.     vfprintf(stderr,str,args);
  1251.     va_end(args);
  1252.     errno=save_e;
  1253.     perror(" ");
  1254.     fflush(stderr);
  1255. }
  1256. #endif
  1257.  
  1258. #ifdef DOPRNT_MSG
  1259. void msg(str,args)
  1260. char *str;
  1261. int args;
  1262. {
  1263.     fflush(msg_file);
  1264.     fprintf(stderr,"%s: ",tar);
  1265.     if(f_sayblock)
  1266.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1267.     _doprnt(str, &args, stderr);
  1268.     putc('\n',stderr);
  1269.     fflush(stderr);
  1270. }
  1271.  
  1272. void msg_perror(str,args)
  1273. char *str;
  1274. {
  1275.     int save_e;
  1276.     extern int errno;
  1277.  
  1278.     save_e=errno;
  1279.     fflush(msg_file);
  1280.     fprintf(stderr,"%s: ",tar);
  1281.     if(f_sayblock)
  1282.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1283.     _doprnt(str, &args, stderr);
  1284.     errno=save_e;
  1285.     perror(" ");
  1286.     fflush(stderr);
  1287. }
  1288.  
  1289. #endif
  1290. #ifdef LOSING_MSG
  1291. void msg(str,a1,a2,a3,a4,a5,a6)
  1292. char *str;
  1293. {
  1294.     fflush(msg_file);
  1295.     fprintf(stderr,"%s: ",tar);
  1296.     if(f_sayblock)
  1297.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1298.     fprintf(stderr,str,a1,a2,a3,a4,a5,a6);
  1299.     putc('\n',stderr);
  1300.     fflush(stderr);
  1301. }
  1302.  
  1303. void msg_perror(str,a1,a2,a3,a4,a5,a6)
  1304. char *str;
  1305. {
  1306.     int save_e;
  1307.     extern int errno;
  1308.  
  1309.     save_e=errno;
  1310.     fflush(msg_file);
  1311.     fprintf(stderr,"%s: ",tar);
  1312.     if(f_sayblock)
  1313.         fprintf(stderr,"rec %d: ",baserec + (ar_record - ar_block));
  1314.     fprintf(stderr,str,a1,a2,a3,a4,a5,a6);
  1315.     fprintf(stderr,": ");
  1316.     errno=save_e;
  1317.     perror(" ");
  1318. }
  1319.  
  1320. #endif
  1321. @
  1322.  
  1323.  
  1324. 1.3
  1325. log
  1326. @"ifdef ds3100" -> "ifdef mips", and use varargs/vfprintf.
  1327. (Mike checking in for John.)
  1328. @
  1329. text
  1330. @d158 1
  1331. d160 1
  1332. a160 1
  1333. getdate(str)
  1334. d162 1
  1335. d1130 2
  1336. d1133 9
  1337. d1176 2
  1338. a1177 1
  1339. #endif
  1340. @
  1341.  
  1342.  
  1343. 1.2
  1344. log
  1345. @Added support for long filenames and long symbolic linkes.
  1346. @
  1347. text
  1348. @d135 2
  1349. a136 2
  1350. #ifdef ds3100
  1351. #undef VARARGS_MSG
  1352. @
  1353.  
  1354.  
  1355. 1.1
  1356. log
  1357. @Initial revision
  1358. @
  1359. text
  1360. @d91 1
  1361. d132 10
  1362. d153 4
  1363. a156 1
  1364. #define OFF_FROM GMT 18000        /* Change for your time zone! */
  1365. d713 1
  1366. a713 1
  1367.         fprintf(stderr,"%s:",myname);
  1368. @
  1369.